package com.michaelbulava.TFS.vcs.revision;

import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.io.StreamUtil;
import com.michaelbulava.TFS.vcs.exceptions.TFSException;
import com.michaelbulava.TFS.vcs.utils.TFSFileUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;

/**
 * Created by MichaelA on 8/5/2015.
 */
public class TFSContentStoreFactory {


    public static TFSContentStore create(URI serverUri, int itemId, int revision) throws IOException{
        return new TFSContentStoreImpl(serverUri, itemId, revision);
    }

    public static TFSContentStore find(URI serverUri, int itemId, int revision) throws IOException{
        return TFSContentStoreImpl.find(serverUri, itemId, revision);
    }



    private static class TFSContentStoreImpl implements TFSContentStore{
        private static final String TMP_FILE_NAME = "idea_tfs";
        private static String myTmpDir;
        private final File myTmpFile;

        TFSContentStoreImpl(URI serverURI, int itemId, int revision) throws IOException{
            this.myTmpFile = new File(createTmpFileName(serverURI, itemId, revision));
            this.myTmpFile.deleteOnExit();
        }

        TFSContentStoreImpl(File tmpFile){
            myTmpFile = tmpFile;
        }


        @Override
        public void saveContent(TFSFileUtil.ContentWriter contentWriter) throws TFSException, IOException {
            TFSFileUtil.setFileContent(myTmpFile, contentWriter);
        }

        @Override
        public byte[] loadContent() throws TFSException, IOException {
            InputStream fileStream = null;
            try {
                fileStream = new FileInputStream(this.myTmpFile);
                return StreamUtil.loadFromStream(fileStream);
            } finally {
                if (fileStream != null)
                    fileStream.close();

            }
        }

        private static String createTmpFileName(URI serverURI, int itemId, int revision) throws IOException {
            return getTmpDir() + File.pathSeparator + serverURI.toASCIIString().hashCode() + "_" +
                    itemId + "." + revision;
        }

        private static String getTmpDir() throws IOException {
            if (myTmpDir == null){
                File tmpDir = FileUtil.createTempFile(TMP_FILE_NAME, "");
                tmpDir.delete();
                tmpDir.mkdir();
                tmpDir.deleteOnExit();
                myTmpDir = tmpDir.getAbsolutePath();
            }
            return myTmpDir;
        }

        public static TFSContentStore find(URI serverUri, int itemId, int revision) throws IOException {
            File tmpFile = new File(createTmpFileName(serverUri, itemId, revision));
            if (tmpFile.exists()) {
                return new TFSContentStoreImpl(tmpFile);
            }
            return null;
        }
    }
}
